import { FieldIgnoreType } from '@/components/FormCreate/src/config/vxetable/UseVxeTableRule'

export interface VxeTableColumn {
  id: any
  field: any
  title: any
  sortable: boolean
  minWidth: number
  type: string
}



export interface VxeQueryColumn {
  field: any
  title: string
  span: number
  folding: boolean
  itemRender: any
}

export const FcVxeControlRenderTypeMapping = {
  input: 'VxeInput',
  datePicker: 'VxeDatePicker',
  timePicker: 'VxeDatePicker',
  textarea: 'VxeTextarea',
  select: 'VxeSelect',
  radio: 'VxeRadio',
  // Checkbox: 'checkbox',
  // Switch: 'switch',
  // Date: 'date',
  // Time: 'time',
  // DateTime: 'datetime',
  // Upload: 'upload',
  // Color: 'color',
  // Editor: 'editor',
  // Password: 'password'
}

export const Fc2VxeRenderMapping = (type: string) => {
  if (FcVxeControlRenderTypeMapping[type]) {
    return FcVxeControlRenderTypeMapping[type]
  }
  return 'VxeInput'
  // return 'Vxe'+ type.charAt(0).toUpperCase() + type.slice(1)
}
export const Vxe2FcMapping = (type: string) => {
  if (FcVxeControlRenderTypeMapping[type]) {
    return FcVxeControlRenderTypeMapping[type]
  }
  return 'input'
}

/**
 * 将formCrate的规则转为vxe-table的columns
 * @param rules
 * @constructor
 */
export const FormCreateRule2Tbl = (rules: Array<VxeTableColumn>) => {
  const newColumnsList: VxeTableColumn[] = [] 
  if(!rules || rules.length === 0){
    return []
  }
  rules.forEach((rule) => {
    if (rule.type === FieldIgnoreType.DataTable) {
      // 序列号控件
      return
    }
    const controlColModel = {
      id: rule._fc_id,
      field: rule.field,
      title: rule.title,
      type: rule.type,
      sortable: true,
      minWidth: 100
    }
    newColumnsList.push(controlColModel)
  })
  return newColumnsList
}

export const DTColumns2FormCondition = (columns: Array<VxeTableColumn>) => {
  const newColumnsList: VxeQueryColumn[] = []
  let foldingIndex = 0 
  columns.forEach((column) => {
    const queryCondition: VxeQueryColumn = {
      field: column.field,
      title: column.title,
      span: 8,
      itemRender: {
        name: Fc2VxeRenderMapping(column.type)
      } // 添加 type 属性
    }
    if (foldingIndex > 3) {
      queryCondition.folding = true
    }
    newColumnsList.push(queryCondition)
    foldingIndex++
  })

  return newColumnsList
}

export interface DTOperator {
  code: string
  name: string
  status: string
}

export const DataTableOperator = {
  add: {
    code: 'add',
    name: '新增',
    status: 'primary'
  },
  edit: {
    code: 'edit',
    name: '编辑'
  },
  save: {
    code: 'save',
    name: '保存'
  },
  delete: {
    code: 'delete',
    name: '删除',
    status: 'danger'
  },
  batchDelete: {
    code: 'batchDelete',
    name: '批量删除',
    status: 'danger'
  },
  export: {
    code: 'export',
    name: '导出'
  }
}

export const DtOperatorConvertGird = (buttons: string[]) => {
  const btnOperator: DTOperator[] = []
  buttons.forEach((btn) => {
    if (DataTableOperator[btn]) {
      btnOperator.push(DataTableOperator[btn])
    }
  })
  return btnOperator
}
